//  [9/10/2013 OMEGA] created

#include <QAbstractItemModel>
#include "DuelAssECommon.h"
#include "DuelAssEProject.h"
#include "DuelAssETranslator.h"
#include "DuelAssEArchiveView.h"
#include "DuelAssEArchiveModel.h"



DAssEArchiveModel::DAssEArchiveModel( DAssEArchiveView* parent ) : 
	mParent(parent)
{

}

DAssEArchiveModel::~DAssEArchiveModel()
{

}

void DAssEArchiveModel::reset( DAssEProject* proj )
{
	clearItems();
	if (proj == NULL)
	{
		return;
	}
	ArchiveList* archList = proj->getArchives();
	foreach(DAssEArchive* arch, *archList)
	{
		appendArchive(arch);
	}
	
}

int DAssEArchiveModel::rowCount( const QModelIndex &parent /*= QModelIndex( ) */ ) const
{
	return mArchiveItems.size();
}

int DAssEArchiveModel::columnCount( const QModelIndex &parent /*= QModelIndex( ) */ ) const
{
	return 2;
}

QVariant DAssEArchiveModel::data( const QModelIndex &index, int role /*= Qt::DisplayRole */ ) const
{
	if (!index.isValid())
		return QVariant();
	if (index.row() >= mArchiveItems.count() || index.row() < 0)
		return QVariant();

	DAssEArchiveModelItem* itm = mArchiveItems.at(index.row());
	if (role == Qt::DisplayRole) 
	{
		if (index.column() == ARCHIVETABLE_HEADER_TYPE)
		{
			return itm->archiveType;
		}
		else if (index.column() == ARCHIVETABLE_HEADER_NAME)
		{
			return itm->archiveName;
		}
	}

	return QVariant();
}

QVariant DAssEArchiveModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
	if (role != Qt::DisplayRole)
		return QVariant();

	if (orientation == Qt::Horizontal) {
		switch (section) {
		case ARCHIVETABLE_HEADER_TYPE:
			return tr("Type");
		case ARCHIVETABLE_HEADER_NAME:
			return tr("Name/Location");
		default:
			return QVariant();
		}
	}

	return QVariant();
}

void DAssEArchiveModel::clearItems()
{
	for (int j = 0; j < mArchiveItems.size(); ++j)
	{
		removeRow(j);
	}

	ArchiveModelItemList::iterator i, iend = mArchiveItems.end();
	for (i = mArchiveItems.begin(); i != iend; ++i)
	{
		delete (*i);
	}
	mArchiveItems.clear();
}

Qt::ItemFlags DAssEArchiveModel::flags( const QModelIndex &index ) const
{
	return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

bool DAssEArchiveModel::setData( const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/ )
{
	if (index.column() == ARCHIVETABLE_HEADER_TYPE)
	{
		mArchiveItems.at(index.row())->archiveType = value.toString();
	}
	if (index.column() == ARCHIVETABLE_HEADER_NAME)
	{
		mArchiveItems.at(index.row())->archiveName = value.toString();
	}

	emit dataChanged(index, index);

	return true;
}




bool DAssEArchiveModel::insertRows( int position, int rows, const QModelIndex &parent /*= QModelIndex()*/ )
{
	
	beginInsertRows(parent, position, position + rows - 1);
	for (int i = 0; i < rows; ++i)
	{
		mArchiveItems.insert(position + i, new DAssEArchiveModelItem());
	}
	endInsertRows();

	return true;
}

bool DAssEArchiveModel::removeRows( int position, int rows, const QModelIndex &parent /*= QModelIndex()*/ )
{
	beginRemoveRows(parent, position, position + rows - 1);
	for (int i = 0; i < rows; ++i)
	{
		DAssEArchiveModelItem* itm = mArchiveItems.takeAt(position);
		delete itm;
	}
	endRemoveRows();

	return true;
}

void DAssEArchiveModel::appendArchive( DAssEArchive* arch )
{
	if (arch == NULL)
	{
		return;
	}

	int i = mArchiveItems.size();
	if (!insertRow(i))
	{
		return;
	}
	QModelIndex typeIdx = index(i, ARCHIVETABLE_HEADER_TYPE);
	setData(typeIdx, DAssETranslator::getArchiveTypeString(arch->getArchiveType()));
	QModelIndex nameIdx = index(i, ARCHIVETABLE_HEADER_NAME);
	setData(nameIdx, arch->getName());

}

void DAssEArchiveModel::removeArchive( DAssEArchive* arch )
{
	ArchiveModelItemList::iterator i, iend = mArchiveItems.end();
	int j = 0;
	for (i = mArchiveItems.begin(); i != iend; ++i, ++j)
	{
		if ((*i)->archiveName == arch->getName())
		{
			removeRow(j);
			return;
		}
	}
}

DAssEArchiveModelItem* DAssEArchiveModel::getArchiveItem( int index )
{
	if (index > mArchiveItems.size() || index < 0)
	{
		return NULL;
	}
	return mArchiveItems.at(index);
}
